import { _decorator, Label, Sprite, SpriteFrame } from 'cc';
import { ListRender } from '../../Basic/Component/ListRender';
import { LoadingManager } from '../../Basic/Manager/LoadingManager';
import { Tool } from '../../Util/Tool';
import { PlayerParams } from '../../Basic/Config/DataConfig';
import { PlayerData } from '../../Basic/Manager/DataManager';
import { RankModel } from './RankModel';
const { ccclass, property } = _decorator;

export class RankData {
  rank: string;

  //   head:string;
}

@ccclass('RankItem')
export class RankItem extends ListRender {
  @property(Sprite)
  item_bg: Sprite;
  @property(Sprite)
  item: Sprite;
  @property(Sprite)
  head: Sprite;
  @property(Label)
  rank: Label;
  @property(Label)
  rankName: Label;
  @property(Label)
  rankDesc: Label;
  @property(Label)
  rankCount: Label;
  dataChanged(): void {
    if (this.data?.rank) {
      if (this.data.avatar.toString().indexOf('http') > -1) {
        LoadingManager.Ins()
          .loadResRemote(this.data.avatar, { ext: '.png' })
          .then((sp) => {
            // console.log('loadResRemote suc', sp);
            this.head.spriteFrame = sp;
          })
          // eslint-disable-next-line @typescript-eslint/no-unused-vars
          .catch((e) => {
            // console.log('loadResRemote err', e);
            RankModel.Ins()
              .loadDynamicRes<SpriteFrame>('public/sprite/visitor/visitor1/spriteFreme')
              .then((spr) => {
                this.head.spriteFrame = spr;
              });
          });
      } else {
        RankModel.Ins()
          .loadDynamicRes<SpriteFrame>('public/sprite/visitor/visitor1/spriteFreme')
          .then((spr) => {
            this.head.spriteFrame = spr;
          });
      }
      this.rank.string = `${this.data.rank}`;
      this.rankName.string = `${this.data.nickname}`;
      this.rankCount.string = `${this.data.score}`;
      this.rankDesc.string = `累计通关数`;
    } else if (this.node.parent.name === 'RankUx') {
      this.rank.string = `-`;
      this.rankName.string = `${PlayerData.get(PlayerParams.nickname)}`;
      this.rankCount.string = `暂无数据`;
      this.rankDesc.string = `累计通关数`;
      if (PlayerData.get(PlayerParams.avatarUrl).indexOf('http') > -1) {
        LoadingManager.Ins()
          .loadResRemote(PlayerData.get(PlayerParams.avatarUrl), { ext: '.png' })
          .then((sp) => {
            // console.log('loadResRemote suc', sp);
            this.head.spriteFrame = sp;
          })
          // eslint-disable-next-line @typescript-eslint/no-unused-vars
          .catch((e) => {
            // console.log('loadResRemote err', e);
            RankModel.Ins()
              .loadDynamicRes<SpriteFrame>('public/sprite/visitor/visitor1/spriteFreme')
              .then((spr) => {
                this.head.spriteFrame = spr;
              });
          });
      } else {
        RankModel.Ins()
          .loadDynamicRes<SpriteFrame>('public/sprite/visitor/visitor1/spriteFreme')
          .then((spr) => {
            this.head.spriteFrame = spr;
          });
      }
    } else {
      this.rank.string = ``;
      this.rankCount.string = ``;
      this.rankName.string = ``;
      this.rankDesc.string = ``;
      this.head.spriteFrame = null;
    }
  }
}
